package com.octopus.kernal.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.MissingResourceException;

import org.apache.commons.lang.StringUtils;

import com.octopus.kernal.model.BaseStruct;
import com.octopus.kernal.service.ConfigService;

/**
 * Command binding object for search entities.
 */
public abstract class AbstractSearchCommand<T extends BaseStruct> {
	
	public static Integer CONFIG_MAX_PAGE_ITEMS;
	
	public static Integer CONFIG_MAX_INDEX_PAGES;
	
	private T entity;
	
	private T entity2;
	
	private T operator;
	
	private Boolean like = false;
	
    private Integer totalNumber = 0;
    
    private Integer offset = 0;
    
    private Integer maxPageItems = 15;
    
    private Integer maxIndexPages = 5;
    
    private String beginDateStr;
    
    private String endDateStr;
    
    private Date beginDate;
    
    private Date endDate;
    
    private String[] propertiesSorted;
    
    public AbstractSearchCommand(){
    	if(CONFIG_MAX_PAGE_ITEMS != null)
    		return;
		try{
			String maxPageItems = ConfigService.getInstance().getProperty("pager.max.page.items");
			String maxIndexPages = ConfigService.getInstance().getProperty("pager.max.index.pages");
			if(StringUtils.isNotBlank(maxPageItems)){
				CONFIG_MAX_PAGE_ITEMS = new Integer(maxPageItems);
			}else{
				CONFIG_MAX_PAGE_ITEMS = -1;
			}
			if(StringUtils.isNotBlank(maxIndexPages)){
				CONFIG_MAX_INDEX_PAGES = new Integer(maxIndexPages);
			}else{
				CONFIG_MAX_INDEX_PAGES = -1;
			}
		}catch(MissingResourceException e){
			CONFIG_MAX_PAGE_ITEMS = -1;
			CONFIG_MAX_INDEX_PAGES = -1;
		}
    }

	public Integer getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(Integer totalNumber) {
		this.totalNumber = totalNumber;
	}

	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getMaxPageItems() {
		if(CONFIG_MAX_PAGE_ITEMS != null && CONFIG_MAX_PAGE_ITEMS != -1)
			return CONFIG_MAX_PAGE_ITEMS;
		else
			return maxPageItems;
	}

	public Integer getMaxIndexPages() {
		if(CONFIG_MAX_INDEX_PAGES != null && CONFIG_MAX_INDEX_PAGES != -1)
			return CONFIG_MAX_INDEX_PAGES;
		else
			return maxIndexPages;
	}
	
	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getBeginDateStr() {
		return beginDateStr;
	}

	public void setBeginDateStr(String beginDateStr) {
		this.beginDateStr = beginDateStr;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("en"));
		if(StringUtils.isNotBlank(beginDateStr))
			try {
				this.beginDate = format.parse(beginDateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
	}

	public String getEndDateStr() {
		return endDateStr;
	}

	public void setEndDateStr(String endDateStr) {
		this.endDateStr = endDateStr;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("en"));
		if(StringUtils.isNotBlank(endDateStr))
			try {
				this.endDate = format.parse(endDateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

	public Boolean getLike() {
		return like;
	}

	public void setLike(Boolean like) {
		this.like = like;
	}

	public String[] getPropertiesSorted() {
		return propertiesSorted;
	}

	public void setPropertiesSorted(String[] propertiesSorted) {
		this.propertiesSorted = propertiesSorted;
	}

	public T getEntity2() {
		return entity2;
	}

	public void setEntity2(T entity2) {
		this.entity2 = entity2;
	}

	public T getOperator() {
		return operator;
	}

	public void setOperator(T operator) {
		this.operator = operator;
	}

	public void setMaxPageItems(Integer maxPageItems) {
		this.maxPageItems = maxPageItems;
	}

	public void setMaxIndexPages(Integer maxIndexPages) {
		this.maxIndexPages = maxIndexPages;
	}
}
